﻿package com.winmastergames.configParser {
	/*
	   Advanced Flash Engine in ActionScript 3.
	   Copyright (C) 2009  Dean Gardiner, Taylor Lodge

	   This program is free software: you can redistribute it and/or modify
	   it under the terms of the GNU General Public License as published by
	   the Free Software Foundation, either version 3 of the License, or
	   (at your option) any later version.

	   This program is distributed in the hope that it will be useful,
	   but WITHOUT ANY WARRANTY; without even the implied warranty of
	   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	   GNU General Public License for more details.

	   You should have received a copy of the GNU General Public License
	   along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 */
	import flash.display.Sprite;
	import flash.errors.*;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import com.winmastergames.configParser.mapObject;
	import com.winmastergames.configParser.objectObject;
	import com.winmastergames.gameassets.enemySpawner;
	import com.winmastergames.lolgame.engine;
	import com.winmastergames.lolgame.gameUtils;

	public class mapParser {
		// TODO: Document This Class on Wiki.
		public static var stream:URLLoader = new URLLoader();
		public static var requesturl:String = "maps/map1/settings.ini";
		public static var fileReadMode:String = "null";
		public static var tempMapObject:mapObject = new mapObject();

		public static function parserMapFile(path:String){
			stream.addEventListener(Event.COMPLETE, mapLoaded);
			try {
				stream.load(new URLRequest(requesturl));
			} catch (e:Error){
				trace(e);
			}
		}

		public static function mapLoaded(e:Event){
			var fileData:String = e.target.data;
			var fileLines:Array = fileData.split("\r\n");
			var fileVarLine:Array;
			var tempLine:String;
			for (var i:Number = 0; i < fileLines.length; i++){
				switch (fileLines[i]){
					case "[settings]":
						fileReadMode = "settings";
						break;
					default:
						tempLine = fileLines[i];
						fileVarLine = tempLine.split("=");
						fileVarLine[0] = cleanVarData(fileVarLine[0]);
						fileVarLine[1] = cleanVarData(fileVarLine[1]);
						switch (fileVarLine[0]){
						case "name":
							tempMapObject.map_name = fileVarLine[1];
							break;
						case "groundtype":
							tempMapObject.map_groundtype = fileVarLine[1];
							break;
						case "objectfile":
							tempMapObject.map_objectfile = fileVarLine[1];
							break;
						case "wallfile":
							tempMapObject.map_wallfile = fileVarLine[1];
							break;
					}
						break;
				}
			}
			parseObjectData(tempMapObject);
		}

		public static function parseObjectData(mapObjectReference:mapObject){
			stream.removeEventListener(Event.COMPLETE, mapLoaded);
			stream.addEventListener(Event.COMPLETE, objectsLoaded);
			try {
				stream.load(new URLRequest(mapObjectReference.map_objectfile));
			} catch (e:Error){
				trace(e);
			}
		}

		public static function objectsLoaded(e:Event){
			var fileData:String = e.target.data;
			var fileLines:Array = fileData.split("\r\n");
			var tempLine:String;
			var fileTempArray:Array;
			var tempObject:objectObject = new objectObject();
			for (var i:Number = 0; i < fileLines.length; i++){
				switch (fileLines[i]){
					case "[objects]":
						fileReadMode = "objects";
						break;
					default:
						tempLine = fileLines[i];
						fileTempArray = tempLine.split(":");
						tempObject.object_index = fileTempArray[0];
						tempObject.object_x = fileTempArray[1];
						tempObject.object_y = fileTempArray[2];
						tempObject.object_width = fileTempArray[3];
						tempObject.object_height = fileTempArray[4];
						switch (fileTempArray[0]){
						case "1":
							tempObject.object_name = "Enemy Spawner";
							break;
						case "2":
							tempObject.object_name = "Player Spawn Point";
							break;
					}
						tempMapObject.map_objects.push(tempObject);
						loadObjects(tempMapObject);
						break;
				}
			}
			parseWallData(tempMapObject);
		}

		public static function loadObjects(map:mapObject){
			for (var i:Number = 0; i < map.map_objects.length; i++){
				var tempObject:objectObject = map.map_objects[i];
				switch (tempObject.object_index.toString()){
					case "1":
						var tempSpawner:enemySpawner = new enemySpawner(engine.st_level_mc, tempObject.object_x, tempObject.object_y, 1, 10);
						break;
					case "2":
						break;
				}
			}
		}

		public static function parseWallData(mapObjectReference:mapObject){
			stream.removeEventListener(Event.COMPLETE, objectsLoaded);
			stream.addEventListener(Event.COMPLETE, wallsLoaded);
			try {
				stream.load(new URLRequest(mapObjectReference.map_wallfile));
			} catch (e:Error){
				trace(e);
			}
		}

		public static function wallsLoaded(e:Event){
			var fileData:String = e.target.data;
			var fileLines:Array = fileData.split("\r\n");
			var tempLine:String;
			var fileTempArray:Array;
			var tempObject:objectObject = new objectObject();
			for (var i:Number = 0; i < fileLines.length; i++){
				switch (fileLines[i]){
					case "[walls]":
						fileReadMode = "walls";
						break;
					default:
						tempLine = fileLines[i];
						fileTempArray = tempLine.split(":");
						tempObject.object_index = fileTempArray[0];
						tempObject.object_x = fileTempArray[1];
						tempObject.object_y = fileTempArray[2];
						tempObject.object_width = fileTempArray[3];
						tempObject.object_height = fileTempArray[4].replace(";", "");
						var tempArrayStr:String = fileTempArray[5].replace(";", "");
						tempArrayStr = tempArrayStr.replace("{", "");
						tempArrayStr = tempArrayStr.replace("}", "");
						if (tempArrayStr.indexOf(",") == true){
							tempObject.object_args = tempArrayStr.split(",");
						} else {
							tempObject.object_args.push(tempArrayStr);
						}
						switch (fileTempArray[0]){
						case "1":
							tempObject.object_name = "Wall";
							break;
					}
						tempMapObject.map_walls.push(tempObject);
						loadWall(tempMapObject);
						break;
				}
			}
		}

		public static function loadWall(map:mapObject){
			for (var i:Number = 0; i < map.map_walls.length; i++){
				var tempObject:objectObject = map.map_walls[i];
				switch (tempObject.object_index.toString()){
					case "1":
						gameUtils.createWall(tempObject.object_x, tempObject.object_y, tempObject.object_width, tempObject.object_height, tempObject.object_args[0]);
						break;
				}
			}
		}

		public static function cleanVarData(input:String):String {
			var tempInput:String = input.replace("\"", "");
			tempInput = tempInput.replace("\"", "");
			return tempInput.replace(";", "");
		}
	}
}